package com.jingyanzi.member.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.jingyanzi.member.domain.IntegralDO;
import com.jingyanzi.member.domain.result.ResultDO;
import com.jingyanzi.member.domain.result.ResultSupport;
import com.jingyanzi.member.request.SingleRequest;
import com.jingyanzi.member.response.SingleResponse;
import com.jingyanzi.member.service.IIntegralLogService;
import com.jingyanzi.member.service.IIntegralService;

@RestController
@RequestMapping("/api/integral")
public class IntegralController {
	@Autowired
	private IIntegralService integralService;
	@Autowired
	private IIntegralLogService integralLogService;
	

	@RequestMapping(value = "/change.json", method = RequestMethod.POST)
	public SingleResponse<IntegralDO> change(SingleRequest<IntegralDO> request) {
		
		IntegralDO integralDO = request.getData();
		
		SingleResponse<IntegralDO> response = new SingleResponse<IntegralDO>();
		
		ResultDO result = integralLogService.addLog(integralDO);
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}

		return response;
	}

	@RequestMapping(value = "/getIntegral.json", method = RequestMethod.POST)
	public SingleResponse<IntegralDO> getIntegral(SingleRequest<IntegralDO> request) {
		ResultDO result = new ResultSupport();
		
		IntegralDO integralDO = request.getData();
		
		SingleResponse<IntegralDO> response = new SingleResponse<IntegralDO>();
		
		result = integralService.get(integralDO.getMemberId());
		if (result.isSuccess()) {
			response.setStatus(200);
			response.setVersion(1);
			response.setMsg("");
			response.setData((IntegralDO) result.getModel(ResultSupport.FIRST_MODEL_KEY));
		} else {
			response.setStatus(500);
			response.setVersion(1);
			response.setMsg("");
		}
		
		return response;
	}
}
